const robot = require('robotjs');
const fs = require('fs');
const { PNG } = require('pngjs');

const Tesseract = require('tesseract.js');
const dayjs=require('dayjs');
function delay(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}

const { execSync } = require('child_process');

//粘贴
function pasteFromClipboard(text) {
    try {
        // macOS 系统
        // 对双引号和反斜杠进行转义
        const escapedText = text.replace(/"/g, '\\"').replace(/\\/g, '\\\\');
        execSync(`echo "${escapedText}" | pbcopy`);
        robot.keyTap('v','command')
    } catch (error) {
        console.error('复制到剪贴板失败:', error);
    }
    // Windows 系统

}
const rectView={
    getValueByPosArr(posArr,rect){
        return rect.data[posArr[0]+posArr[1]*rect.width]
    },
    //通过花费得到的收益
    initItem(item,all){
        const rect1=this.rect1
        const rect2=this.rect2
        const posArr1=this.posArr1
        const posArr2=this.posArr2

        while(item.price>0){
            const pos1=[posArr1[item.id][0]+posArr2[item.curFee][0],posArr1[item.id][1]+posArr2[item.curFee][1]]
            const pos2=posArr2[item.curFee]
            item.curFee++
            item.price--
            if(this.getValueByPosArr(pos1,rect1)===this.getValueByPosArr(pos2,rect2)){
                item.val++
            }else if(!all){
                break
            }else if(all!==true){
                all--
            }
        }
    },
    //大图中查找小图,获取最大的相似区域
    getMaxList(rect1,rect2,pipei){
        this.rect1=rect1
        this.rect2=rect2
        this.posArr1=[]
        this.posArr2=[]
        this.taskList=[]
        const posArr1=this.posArr1
        const posArr2=this.posArr2


// 初始化任务列表
        let taskList=this.taskList
        let id = 0;

        const len2=rect2.width*rect2.height
// 遍历 bigRect 的每个像素点，生成任务
        const h1=rect1.height-rect2.height+1
        const w1=rect1.width-rect2.width+1
        for (let y = 0; y < h1; y++) {
            for (let x = 0; x < w1; x++) {
                posArr1.push([x,y])
                taskList.push({
                    id: id,          // 唯一任务 ID
                    price: len2,     // 价格
                    curFee:0,   //花费
                    val:0,    //价值
                });
                id++
            }
        }
        for (let y = 0; y < rect2.height; y++) {
            for (let x = 0; x < rect2.width; x++) {
                posArr2.push([x,y])
            }
        }

        //一直找到val最大的，
        let maxVal=0

        let isRunning=true

        while (isRunning){
            let maxList=[]
            taskList.forEach( (item)=>{
                this.initItem(item)
                if (item.val > maxVal) {
                    maxVal = item.val
                    maxList=[item]
                }else if (item.val === maxVal) {
                    maxList.push(item)
                }
            })
            if(taskList.length===maxList.length){
                maxList=[]
                taskList.forEach((item)=>{
                    this.initItem(item,10)
                    if (item.val > maxVal) {
                        maxVal = item.val
                        maxList=[item]
                    }else if (item.val === maxVal) {
                        maxList.push(item)
                    }
                })
            }
            if(taskList.length===maxList.length||maxList.length==0){
                isRunning=false
            }else{
                taskList=maxList
            }

        }

        if(taskList.length){
            taskList.forEach((item)=>{
                item.x=posArr1[item.id][0]
                item.y=posArr1[item.id][1]
                item.width=rect2.width
                item.height=rect2.height
                item.pipei=parseInt((item.val*100/item.curFee))
            })
        }
        return taskList


    },

}
const screenWidth=2560
const screenHeight=1600
const baseView={

    captureFullScreen(){
        const width=2560
        const height=1600
        // 截取指定区域的屏幕
        const screenshot = robot.screen.capture(0, 0, width, height);

        // 创建 PNG 对象
        const png = new PNG({ width, height });

        // 将截图数据复制到 PNG 对象
        for (let yIndex = 0; yIndex < height; yIndex++) {
            for (let xIndex = 0; xIndex < width; xIndex++) {
                const pos = (yIndex * width + xIndex) * 4;
                const imgPos = (yIndex * width + xIndex) * 4;
                // 注意：robotjs 返回的像素数据是 BGRA 格式，需要转换为 RGBA
                const b = screenshot.image.readUInt8(imgPos);
                const g = screenshot.image.readUInt8(imgPos + 1);
                const r = screenshot.image.readUInt8(imgPos + 2);
                const a = screenshot.image.readUInt8(imgPos + 3);

                png.data[pos] = r; // Red
                png.data[pos + 1] = g; // Green
                png.data[pos + 2] = b; // Blue
                png.data[pos + 3] = a; // Alpha
            }
        }
        fs.writeFileSync('./img/screenshot.png', PNG.sync.write(png));
    },
    screenPng:null,
    getScreenPng(){
        if(this.screenPng){
            return this.screenPng
        }
        const width=2560
        const height=1600
        // 截取指定区域的屏幕
        const screenshot = robot.screen.capture(0, 0, width, height);

        // 创建 PNG 对象
        const png = new PNG({ width, height });

        // 将截图数据复制到 PNG 对象
        for (let yIndex = 0; yIndex < height; yIndex++) {
            for (let xIndex = 0; xIndex < width; xIndex++) {
                const pos = (yIndex * width + xIndex) * 4;
                const imgPos = (yIndex * width + xIndex) * 4;
                // 注意：robotjs 返回的像素数据是 BGRA 格式，需要转换为 RGBA
                const b = screenshot.image.readUInt8(imgPos);
                const g = screenshot.image.readUInt8(imgPos + 1);
                const r = screenshot.image.readUInt8(imgPos+2);
                const a = screenshot.image.readUInt8(imgPos + 3);

                png.data[pos] = r; // Red
                png.data[pos + 1] = g; // Green
                png.data[pos + 2] = b; // Blue
                png.data[pos + 3] = a; // Alpha
            }
        }
        // fs.writeFileSync('./img/screenshot.png', PNG.sync.write(png));
        this.screenPng=png
        return png
    },
    async hideAllWindows(){
        // console.log('hideAllWindows')
        // // 获取屏幕尺寸
        // const screenSize = robot.getScreenSize();
        // const width = screenSize.width;
        // const height = screenSize.height;
        // // console.log('屏幕宽度:', width, '屏幕高度:', height);
        // this.moveMouse(width,height)
        // this.mouseClick()
    },
    getRectInScreen(imgInfo){
        if(!imgInfo){
            imgInfo={x:0,y:0,width:screenWidth,height:screenHeight}
        }
       const t1Png = this.getScreenPng();
        const height=imgInfo.height
        const width=imgInfo.width
        const rect={
            width: width,
            height: height,
            data:[]
        }
        for(let y=0;y<height;y++){
            for (let x = 0; x < width; x++) {
                const pos = (y * width + x);
                const imgPos = ((imgInfo.y+y) * t1Png.width + x+imgInfo.x) * 4;
                const r=t1Png.data[imgPos]; // Red
                const g=t1Png.data[imgPos + 1]; // Green
                const b=t1Png.data[imgPos + 2]; // Blue
                rect.data[pos]=parseInt((r+b+g)/3)
            }
        }
        return rect
    },
    //获取屏幕截图
    getCroppedPngInLocal(imgInfo,f1){
        const t1Buffer = fs.readFileSync(f1);
        const t1Png = PNG.sync.read(t1Buffer);
        const height=imgInfo.height
        const width=imgInfo.width
        // 根据 imgInfo 裁剪截图
        const croppedPng = new PNG({
            width: width,
            height: height
        });
        for (let y = 0; y < height; y++) {
            for (let x = 0; x < width; x++) {
                const pos = (y * width + x) * 4;
                const imgPos = ((imgInfo.y+y) * t1Png.width + x+imgInfo.x) * 4;
                croppedPng.data[pos] = t1Png.data[imgPos]; // Red
                croppedPng.data[pos + 1] = t1Png.data[imgPos+1]; // Green
                croppedPng.data[pos + 2] = t1Png.data[imgPos+2]; // Blue
                croppedPng.data[pos + 3] = t1Png.data[imgPos+3]; // Alpha
            }
        }

        return croppedPng
    },
    //获取屏幕截图
    getCroppedPngInScreen(imgInfo){
       const t1Png = this.getScreenPng();
        const height=imgInfo.height
        const width=imgInfo.width
        // 根据 imgInfo 裁剪截图
        const croppedPng = new PNG({
            width: width,
            height: height
        });
        for (let y = 0; y < height; y++) {
            for (let x = 0; x < width; x++) {
                const pos = (y * width + x) * 4;
                const imgPos = ((imgInfo.y+y) * t1Png.width + x+imgInfo.x) * 4;
                croppedPng.data[pos] = t1Png.data[imgPos]; // Red
                croppedPng.data[pos + 1] = t1Png.data[imgPos+1]; // Green
                croppedPng.data[pos + 2] = t1Png.data[imgPos+2]; // Blue
                croppedPng.data[pos + 3] = t1Png.data[imgPos+3]; // Alpha
            }
        }

        return croppedPng
    },
    getRectInLocal(f1){
        const t1Buffer = fs.readFileSync(f1);
        const t1Png = PNG.sync.read(t1Buffer);
        const height=t1Png.height
        const width=t1Png.width
        const rect={
            width: width,
            height: height,
            data:[]
        }
        for (let y = 0; y < height; y++) {
            for (let x = 0; x < width; x++) {
                const pos = y * width + x;
                const imgPos = pos * 4;
                const r=t1Png.data[imgPos]
                const g=t1Png.data[imgPos + 1]
                const b=t1Png.data[imgPos + 2]
                rect.data[pos]=parseInt((r+b+g)/3)
            }
        }
        return rect
    },
    compareRect(bigRect,smallRect){
        const bigData=bigRect.data
        const smallData=smallRect.data
        let num=0
        let len=0
        for (let y = 0; y < smallRect.height; y++) {
            for (let x = 0; x < smallRect.width; x++) {
                const pos = (y * bigRect.width + x)
                if(bigData[pos]===smallData[pos]){
                    num++
                }
                len++
            }
        }
        return (num*100/len)
    },

    async getTextByImgInScreenShot(imgInfo,img){
        const croppedPng=this.getCroppedPngInScreen(imgInfo)
        // 将 PNG 对象转换为 Uint8Array
        const pngBuffer = PNG.sync.write(croppedPng);
        // if('./img/page-item1.png'===img){
        //     fs.writeFileSync('./img/item1-test.png', pngBuffer);
        // }
        // if('./img/page-arbr.png'===img){
        //     fs.writeFileSync('./img/arbr-test.png', pngBuffer);
        // }
        // if('./img/page-macd.png'===img){
        //     fs.writeFileSync('./img/macd-test.png', pngBuffer);
        // }
        // if('./img/page-kdj.png'===img){
        //     fs.writeFileSync('./img/kdj-test.png', pngBuffer);
        // }
        //识别屏幕中imgInfo位置的文字
        const res=await Tesseract.recognize(
            new Uint8Array(pngBuffer),
            'eng',
        )
        return res.data.text.trim()
    },

}

const config={
    imgInfoMap:{},
}


const view={
    async moveMouse(x,y){
        robot.moveMouse(parseInt(x/2)+1,parseInt(y/2)+1)
        await delay(100);
        robot.moveMouse(parseInt(x/2),parseInt(y/2))
    },
    //获取图片信息在屏幕中的位置
    getImgInfoInScreenShot(f1){
        const imgInfo=config.imgInfoMap[f1]
        const smaRect=this.getRectInLocal(f1)

        const bigRect=this.getRectInScreen(imgInfo)
        const pipei =this.compareRect(bigRect,smaRect)
        console.log('是否匹配',pipei,imgInfo.x,imgInfo.y,pipei)
        if(pipei>90){
            return imgInfo
        }else{
            console.log('匹配图片失败',f1)

        }
    },
    //识别图片位置中的文字
    async getTextByImg(img){
        const topImgInfo=config.imgInfoMap[img]
        return await this.getTextByImgInScreenShot(topImgInfo,img)
    },
    //列表可用的
    codeMap:{
        'SA601':{
            code:'SA601',
            codeName:'纯碱601',
            timeType:2,
        },
        'UR509':{
            code:'UR509',
            codeName:'尿素509',
            timeType:1,//只有白天有
        },
        'SH509':{
            code:'SH509',
            codeName:'烧碱509',
            timeType:2,
        },
        'SA509':{
            code:'SA509',
            codeName:'纯碱509',
            timeType:2,
        },
        'ao2509':{
            code:'ao2509',
            codeName:'氧化铝2509',
            timeType:3,
        },
    },
    isAutoPage:false,
    async showQHPage(){

        const imgInfo=await this.clickImgInScreen('./img/qhicon.png')
        // console.log('imgInfo',imgInfo)
        // await delay(2000)
        if(imgInfo){
            this.isAutoPage=this.getImgInfoInScreenShot('./img/page-top.png')
        }else{
            console.log('打开期货app失败')
        }
    },
    async login(task){
        const imgInfo=this.getImgInfoInScreenShot(task.img)
        if(imgInfo){
            await this.moveMouse(imgInfo.x+imgInfo.width/2,imgInfo.y+imgInfo.height-20)
            this.mouseClick()
        }else{
            await delay(1000)
            await this.login(task)
        }
    },
    mouseClick(){
        this.screenPng=null
        robot.mouseClick()
    },
    async initConfig(){

        if(fs.existsSync('./config.json')){
            const cacheConfig=JSON.parse(fs.readFileSync('./config.json','utf-8'))
            Object.assign(config,cacheConfig)
        }
        // await this.initOneImg('./img/page-price.png','./img/page-screen.png','imgInfo')
        // fs.writeFileSync('./config.json',JSON.stringify(config,null,2))

        if(!fs.existsSync('./config.json')){
            await this.initOneImg('./img/qqmsg.png','./img/qqmsg-screen.png')

            await this.initOneImg('./img/qhicon.png','./img/icons-screen.png')
            await this.initOneImg('./img/qqicon.png','./img/icons-screen.png')


            await this.initOneImg('./img/page-item.png','./img/page-screen.png')
            await this.initOneImg('./img/page-macd.png','./img/page-screen.png')
            await this.initOneImg('./img/page-kdj.png','./img/page-screen.png')
            await this.initOneImg('./img/page-arbr.png','./img/page-screen.png')
            await this.initOneImg('./img/page-code.png','./img/page-screen.png')
            await this.initOneImg('./img/page-price.png','./img/page-screen.png')

            await this.initOneImg('./img/page-top.png','./img/page-screen.png')

            fs.writeFileSync('./config.json',JSON.stringify(config,null,2))
            console.log('配置ok,生成图片信息')
        }

    },
    async initOneImg(img,screenImg){
        const smaRect=this.getRectInLocal(img)
        const screenRect=this.getRectInLocal(screenImg)
        const list=rectView.getMaxList(screenRect,smaRect)
        console.log(img,list[0].pipei,list.length)
        // const png=this.getCroppedPngInLocal(list[0],screenImg)
        // fs.writeFileSync('./img/test.png', PNG.sync.write(png));
        if(list.length===1&&list[0].pipei>98){
            const {x,y,width,height}=list[0]
            config.imgInfoMap[img]={x,y,width,height}
        }else{
            console.log(list)
            throw new Error('图片匹配失败'+img)
        }
    },
    async clickImgInScreen(img){
        // let imgInfo=this.getImgInfoInScreenShot(img)
        const imgInfo=config.imgInfoMap[img]
        if(imgInfo){
            await this.moveMouse(imgInfo.x+imgInfo.width/2,imgInfo.y+imgInfo.height/2)
            this.mouseClick()
            return imgInfo
        }else{
            throw new Error('图片匹配失败'+img)
        }
    },
    msgList:[],

    async qqSendMsg(text=''){

        await this.hideAllWindows()
        const has1=await this.clickImgInScreen('./img/qqicon.png')
        await delay(200)

        if(has1){
            await this.clickImgInScreen('./img/qqmsg.png')
            pasteFromClipboard(text)

            await delay(500)
            if(!this.isDev){
                robot.keyTap('enter')
            }
            await delay(500)
        }
    },

    //delayEmptyTime
    async delayEmptyTime(){
        const now = dayjs();
        const nowTime = now.valueOf();
        const day = dayjs().day();

        const nineAm = dayjs(now.format('YYYY-MM-DD 09:00:00')).valueOf();
        const elevenThirtyAm = dayjs(now.format('YYYY-MM-DD 11:30:00')).valueOf();
        const oneThirtyPm = dayjs(now.format('YYYY-MM-DD 13:30:00')).valueOf();
        const threePm = dayjs(now.format('YYYY-MM-DD 15:00:00')).valueOf();
        const ninePm = dayjs(now.format('YYYY-MM-DD 21:00:00')).valueOf();
        const oneAm = dayjs(now.format('YYYY-MM-DD 01:00:00')).valueOf();
        const nextDayNineAm = dayjs(now.add(1, 'day').format('YYYY-MM-DD 09:00:00')).valueOf();
        let durTime=0
        if(day === 6 || day === 0){
            durTime = nextDayNineAm - nowTime;
            console.log('等待第二天09:00', durTime);
        }else if (nowTime > oneAm&&nowTime < nineAm) {
             durTime = nineAm - nowTime;
            console.log('等待9:00', durTime);
        } else if (nowTime > elevenThirtyAm && nowTime < oneThirtyPm) {
             durTime = oneThirtyPm - nowTime;
            console.log('等待13:30', durTime);
        } else if (nowTime > threePm && nowTime < ninePm) {
             durTime = ninePm - nowTime;
            console.log('等待21:00', durTime);
        }
        if(durTime>0){
            await delay(durTime);
            console.log('等待后，程序启动')
            await this.showQHPage()
            await this.qqSendMsg('等待后，程序启动');
            await this.showQHPage();
        }
    },
    async _getSignalData(code,codeName){
        const macdText=await this.getTextByImg('./img/page-macd.png')
        const kdjText=await this.getTextByImg('./img/page-kdj.png')
        const arbrText=await this.getTextByImg('./img/page-arbr.png')
        const price=await this.getTextByImg('./img/page-price.png')

        const signal={
            DIFF:'',
            DEA:'',
            MACD:'',
            K:'',
            D:'',
            J:'',
            AR:'',
            BR:'',
            price
        }
        // 优化正则表达式，精确匹配 DIF、DEA、MACD 的数值
        const macdMatches = macdText.match(/DIF:([-+]?\d*\.\d+|[-+]?\d+).*DEA:([-+]?\d*\.\d+|[-+]?\d+).*MACD:([-+]?\d*\.\d+|[-+]?\d+)/);
        if (macdMatches) {
            signal.DIFF = Number(macdMatches[1]);
            signal.DEA = Number(macdMatches[2]);
            signal.MACD = Number(macdMatches[3]);
        }

        // 优化正则表达式，精确匹配 K、D、J 的数值
        const kdjMatches = kdjText.match(/K:([-+]?\d*\.\d+|[-+]?\d+).*D:([-+]?\d*\.\d+|[-+]?\d+).*J:([-+]?\d*\.\d+|[-+]?\d+)/);
        if (kdjMatches) {
            signal.K = Number(kdjMatches[1]);
            signal.D = Number(kdjMatches[2]);
            signal.J = Number(kdjMatches[3]);
        }
        // 一次性提取 BR 和 AR 的值
        const matches = arbrText.match(/BR:([\d.]+).*AR:([\d.]+)/);
        signal.BR=matches ? Number(matches[1]) : null;
        signal.AR=matches ? Number(matches[2]) : null;

        if(this.isLog){
            console.log(macdText,kdjText,arbrText,signal)
        }
        return signal
    },
    async getSignalData(task){
        let {
            DIFF, DEA, MACD,
            K, D, J,
            AR, BR,price
        }=await this._getSignalData(task)
        let tip=""
        //提示冲顶，准备空
        if(MACD>=3&&DEA>MACD&&DIFF>DEA+1){
            tip=`提示冲顶，准备空 MACD:${MACD} ${DEA} ${DIFF}\n`
        }
        //提示到顶，空
        if(MACD>=1&&MACD<3&&DEA>MACD&&DIFF>DEA+1){
            tip=`提示到顶，空 MACD:${MACD} ${DEA} ${DIFF}\n`
        }
        //提示急跌，空
        if(MACD<-1&&DEA>1&&DIFF>DEA-1){
            tip=`提示急跌，空 MACD:${MACD} ${DEA} ${DIFF}\n`
        }

        //提示冲底，准备多
        if(MACD<=-3&&DEA<MACD&&DIFF<DEA-1){
            tip=`提示冲底，准备多 MACD:${MACD} ${DEA} ${DIFF}\n`
        }
        //提示到底，多
        // MACD=-2.09
        // DEA=-2.418
        // DIFF=-3.463
        if(MACD<=-1&&MACD>-3&&DEA<MACD&&DIFF<DEA-1){
            tip+=`提示到底，多 MACD:${MACD} ${DEA} ${DIFF}\n`
        }

        //提示急涨，多
        if(MACD>1&&DEA<-1&&DIFF<DEA+1){
            tip+=`提示急涨，多 MACD:${MACD} ${DEA} ${DIFF}\n`
        }
        //情绪过低，多
        if(AR<50&&BR<50){
            tip+=`情绪过低，多 ARBR:${AR} ${BR}\n`
        }
        //情绪过高，空
        if(AR>250&&BR>250){
            tip+=`情绪过高，空 ARBR:${AR} ${BR}\n`
        }
        //kdj超卖，多
        // if(K+D+J<30){
        //     tip+=`提示超卖，多 KDJ:${K} ${D} ${J}\n`
        // }
        // //kdj超买，空
        // if(K+D+J>270){
        //     tip+=`提示超买，空 KDJ:${K} ${D} ${J}\n`
        // }
        if(this.isTestSignal){
            this.isTestSignal=false
            tip+=`测试 MACD:${MACD} ${DEA} ${DIFF}`
            tip+=`测试 ARBR:${AR} ${BR}`
        }

        if(tip){
            return {
                time:new Date().getTime(),
                tip:tip,
                code:task.code,
                price,
                codeName:task.codeName,
            }

        }
    },
    isTestSignal:false,
    preSignalMap:{},
    codeList:[],
    async listenAllCodeInit(task){
        const codeList=this.codeList
        if(this.isAutoPage){
            console.log('进入期货界面')
            const listLen=10
            const imgInfo0 = config.imgInfoMap['./img/page-item.png'];
            const repeatArr=[]
            for(let i=0;i<listLen;i++){
                const imgInfo = {
                    x:imgInfo0.x,
                    y:imgInfo0.y+imgInfo0.height*i-i*2,
                    width:imgInfo0.width,
                    height:imgInfo0.height,
                };
                await this.moveMouse(imgInfo.x + imgInfo.width / 2, imgInfo.y + imgInfo.height / 2);
                this.mouseClick();
                await delay(500);
                const text=await this.getTextByImg('./img/page-code.png')
                const arr=text.split(' ')
                let code=arr[0]
                if(arr[0]==='202509'){
                    code='ao2509'
                }
                console.log('识别list：',code)
                if(repeatArr.indexOf(code)>-1){
                    break
                }else{
                    repeatArr.push(code)
                }
                if(this.codeMap[code]){
                    this.codeMap[code].imgInfo=imgInfo
                    const taskItem=this.codeMap[code]
                    codeList.push(taskItem)

                    const signal = await this.getSignalData(taskItem);
                    if (signal) {
                        console.log('存在信号', signal);
                        this.msgList.push(signal);
                        this.preSignalMap[taskItem.code] = signal;
                        //发送qq消息
                        await this.sendMSG()
                    }
                }

            }
        }
    },
    async listenAllCodeList(task){
        while (this.isAutoPage) {
            const codeList = this.codeList;
            const curTime = dayjs().format('HH:mm:ss');
            for (let i = 0; i < codeList.length; i++) {
                const taskItem = codeList[i];
                if(taskItem.timeType===1&&!(curTime>'09:00:00'&&curTime<'15:00:00')){
                    continue
                }
                if(taskItem.timeType===2&&!(curTime>'09:00:00'&&curTime<'23:00:00')){
                    continue
                }
                if(taskItem.timeType===3&&curTime<'09:00:00'&&curTime>'01:00:00'){
                    continue
                }
                const imgInfo = taskItem.imgInfo;
                await this.moveMouse(imgInfo.x + imgInfo.width / 2, imgInfo.y + imgInfo.height / 2);
                this.mouseClick();
                await delay(1000);
                console.log('监听：',taskItem.code);
                const signal = await this.getSignalData(taskItem);
                if (signal) {
                    console.log('存在信号', signal);
                    const preSignal = this.preSignalMap[taskItem.code];
                    this.msgList.push(signal);
                    if (!preSignal || preSignal.time+10000<signal.time) {
                        //发送qq消息
                        await this.sendMSG()
                    }
                    this.preSignalMap[taskItem.code] = signal;

                }
            }
            await this.sendMSG()
            this.isAutoPage = this.getImgInfoInScreenShot('./img/page-top.png');
            await this.delayEmptyTime();
        }
    },
    //间隔1分钟发送一次信号
    async sendMSG(){
        if (this.msgList.length) {
            const tarr = ['v1.01:'+dayjs().format('YYYY-MM-DD HH:mm:ss ')];
            this.msgList.forEach(({codeName, tip,price}) => {
                tarr.push(`\n${codeName} 价格：${price}\n${tip}`);
            });
            this.msgList = [];
            await this.qqSendMsg(tarr.join(' '));
            await this.showQHPage();

        }
    },
    isDev:false,
    isLog:false,
    async init(){
        await this.initConfig()
        await this.delayEmptyTime()
        await this.showQHPage()
        await this.listenAllCodeInit()
        await this.delayEmptyTime()
        await this.listenAllCodeList()
        // // await this.qqSendMsg('测试信息')
        // await this.hideAllWindows()
        console.log('程序退出结束')
    },
}
Object.assign(view,baseView)
view.init()

